package com.tuo51.childcare.service.impl;

import com.tuo51.childcare.service.QRCodeService;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QRCodeServiceImpl implements QRCodeService {

    @Autowired
    WxMpService wxMpService;

    @Override
    public String qrCodeBySceneIdAndObjId(String appid, int sceneId, String objId) {
        WxMpService mpService = wxMpService.switchoverTo(appid);

        WxMpQrcodeService qrcodeService = mpService.getQrcodeService();

        try {
            WxMpQrCodeTicket ticket = qrcodeService.qrCodeCreateLastTicket(sceneId);
            String url = qrcodeService.qrCodePictureUrl(ticket.getTicket());
            return url;
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.UNDEFINED, "获取微信开放平台ticket失败");
        }
    }
}
